package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.TbBrand;
import com.bwie.service.TbBrandService;
import com.bwie.mapper.TbBrandMapper;
import com.bwie.utils.Result;
import com.bwie.utils.TokenUtils;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lenovo
 * @description 针对表【tb_brand(品牌表)】的数据库操作Service实现
 * @createDate 2025-06-20 15:27:23
 */
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand>
        implements TbBrandService {

    @Autowired
    TbBrandMapper tbBrandMapper;
    @Autowired
    HttpServletRequest request;

    @Override
    public Result getBrandList(PageInfoVo pageInfoVo) {
        //构造分页参数
        Page<TbBrand> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        //分页查询
        QueryWrapper<TbBrand> wrapper = new QueryWrapper<>();
        //根据时间倒序排序
        wrapper.lambda().orderByDesc(TbBrand::getCreatedTime);
        //根据名称模糊查询
        if (StringUtils.isNotEmpty(pageInfoVo.getKeyWord())) {
            wrapper.lambda().like(TbBrand::getBrandName, pageInfoVo.getKeyWord());
        }
        Page<TbBrand> tbBrandPage = tbBrandMapper.selectPage(page, wrapper);

        return Result.ok().setData(tbBrandPage);
    }

    @Override
    public Result addBrand(TbBrand tbBrand) {
        //判断名称是否唯一
        TbBrand brand = tbBrandMapper.selectOne(
                new QueryWrapper<TbBrand>().lambda().eq(TbBrand::getBrandName, tbBrand.getBrandName())
        );
        if (brand != null) {
            return Result.error().setMsg("品牌已经存在");
        }
        //设置审计字段
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error().setMsg("请先登录");
        }
        String userName = TokenUtils.tokenUtilsGetName(token);
        tbBrand.setCreatedBy(userName);
        tbBrand.setCreatedTime(new Date());
        //添加
        tbBrandMapper.insert(tbBrand);
        return Result.ok().setData(tbBrand);
    }

    @Override
    public Result updateBrandStatus(TbBrand tbBrand) {
        TbBrand brand = tbBrandMapper.selectById(tbBrand.getBrandId());
        if (brand == null) {
            return Result.error().setMsg("品牌不存在");
        }
        tbBrand.setBrandStatus(tbBrand.getBrandStatus());
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error().setMsg("请先登录");
        }
        String userName = TokenUtils.tokenUtilsGetName(token);
        tbBrand.setUpdatedBy(userName);
        tbBrand.setUpdatedTime(new Date());
        tbBrandMapper.updateById(tbBrand);
        return Result.ok();
    }

    @Override
    public Result brandDelete(Integer brandId) {
        //判断品牌是否存在
        TbBrand tbBrand = tbBrandMapper.selectById(brandId);
        if (tbBrand == null) {
            return Result.error().setMsg("品牌不存在");
        }
        tbBrandMapper.deleteById(brandId);
        return Result.ok();
    }

    @Override
    public Result batchDeleteBrand(List<TbBrand> brandList) {
        List<Integer> ids = brandList.stream().map(item -> item.getBrandId()).collect(Collectors.toList());
        tbBrandMapper.deleteBatchIds(ids);
//        for (TbBrand tbBrand : brandList) {
//            tbBrandMapper.deleteById(tbBrand.getBrandId());
//        }
        return Result.ok();
    }

    @Override
    public Result getBrandAll() {
        List<TbBrand> tbBrandList = tbBrandMapper.selectList(null);
        return Result.ok().setData(tbBrandList);
    }
}




